<!DOCTYPE html>
<html lang="en">
<head>
    {{include file="lib/page_head" /}}
    <style>
        table th{word-wrap:break-word;word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        table td{word-wrap:break-word;word-wrap:break-word;word-break:break-all;}
        #edit_info .form-beautify-select{display:block;width: 100%!important;}
        .form-group{margin-bottom: 5px!important;}
    </style>
</head>
<body >

<!-- Preloader -->
<div id="preloader" style="display: none;">
    <div id="status"><i class="fa fa-spinner fa-spin"></i></div>
</div>

<section>
    {{include file="lib/page_left" /}}
    <div class="mainpanel">

        {{include file="lib/page_header" /}}

        <div class="contentpanel" >
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-btns">
                        <a href="" class="panel-close">×</a>
                        <a href="" class="minimize">−</a>
                    </div>
                    <h4 class="panel-title">代理列表</h4>
                </div>
                <div class="panel-body">
                    <div class="form-inline"  id="info_list">
                        <div class="btn-group">
                            <button class="btn btn-primary" type="button" onclick="edit_info(0)">增加</button>
                            <button class="btn btn-primary" type="button" onclick="location.href = location.href;">刷新</button>
                        </div>

                        <div class="layui-form-item" style="margin-top:10px;">
                            <form class="layui-form" id="search_form" action="" onsubmit="return false;">
                                <div class="form-group">
                                    <label >客户名称</label>
                                    <input class="form-control" name="agent_name"  autocomplete="off" type="text">

                                </div>


                                <div style="display: inline-block;clear: both;">
                                    <button class="btn btn-primary" onclick="search()">查询</button>
                                    <button class="btn btn-default" onclick="search_reset()">取消</button>
                                </div>
                            </form>

                        </div>
                    </div>

                </div>
            </div>
        </div><!-- contentpanel -->

    </div><!-- mainpanel -->
    {{include file="lib/page_right" /}}
</section>

<div style="display: none;" id="edit_info">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">
        <div class="form-group">
            <label > 代理名称<span style="color:red;">*</span></label>
            <input type="text" name="agent_name"  placeholder="代理名称" class="form-control" >
        </div>
        <div class="form-group">
            <label > 代理备注</label>
            <input type="text" name="agent_info"  placeholder="代理备注" class="form-control" data-ignore="true">
        </div>
        <div class="form-group">
            <label > 选择类别<span style="color:red;">*</span></label>
            <select class="select5" name="agent_type"  >
                <option value="0">员工</option>
                <option value="1">代理</option>
            </select>
        </div>
        <div class="form-group">
            <label > 卡费</label>
            <input type="text" name="card_fee" value="0"  placeholder="卡费" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > 运费</label>
            <input type="text" name="freight" value="0"  placeholder="运费" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > 专属卡代理折扣</label>
            <input type="text" name="money_discount" value="0.97"  placeholder="专属卡代理折扣" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > 专属卡代理佣金折扣</label>
            <input type="text" name="rebate_discount" value="0.01"  placeholder="专属卡代理佣金折扣" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > 普通卡代理折扣</label>
            <input type="text" name="money_discount_0" value="0.00"  placeholder="普通卡代理折扣" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > 普通卡代理佣金折扣</label>
            <input type="text" name="rebate_discount_0" value="0.00"  placeholder="普通卡代理佣金折扣" class="form-control"  data-ignore="true">
        </div>
        <div class="form-group">
            <label > KEY(<a href="javascript:void 0;" onclick="make_key(this)">生成KEY</a> )</label>
            <input type="text" name="sign_key"  placeholder="KEY" class="form-control"  data-ignore="true">
        </div>
        <div  class="form-group">
            <label > 状态</label>
            <select class="select5" name="status">
                <option value="0">正常</option>
                <option value="1">禁用</option>
            </select>
        </div>

    </form>
</div>
<div style="display: none;" id="set_credit">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">
        <div class="form-group">
            <label > 授信金额（正数增加，负数递减）<span style="color:red;">*</span></label>
            <input type="number" name="credit"  placeholder="授信金额" class="form-control" >
        </div>
    </form>
</div>
<div style="display: none;" id="set_balance">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">
        <div class="form-group">
            <label > 加款金额（正数增加，负数递减）<span style="color:red;">*</span></label>
            <input type="number" name="balance"  placeholder="加款金额" class="form-control" >
        </div>
    </form>
</div>
<div id="weixin_code_url" style="display:none;background: #fff;text-align: center;padding:20px;">
    <div style="margin:20px;" class="name">微信扫一扫</div>
    <div style="width:200px;height:200px;margin:0 auto;" class="qrcode">

    </div>

</div>
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/jquery-ui-1.10.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jquery.sparkline.min.js"></script>
<script src="../js/toggles.min.js"></script>
<script src="../js/jquery.cookies.js"></script>
<script src="../js/jquery.tmpl.js"></script>
<script src="../js/custom.js"></script>
<script src="../layui/layui.js"></script>
<script src="../js/jquery.gritter.min.js"></script>
<script src="../js/form.beautify.js?t={{$version}}"></script>
<script src="../js/commen.js?t={{$version}}"></script>
<script src="../js/datatables.js?t={{$version}}"></script>
<link href="../css/ley.dialog.css" rel="stylesheet">
<script src="../js/ley.dialog.js?t={{$version}}"></script>
<script src="../js/jquery.qrcode.min.js"></script>
<script src="../js/md5.js"></script>
<script>
    function formatState (state) {
        if (!state.id) { return state.text; }
        var $state = $(
            '<span><span class="fa ' + state.element.value.toLowerCase() + '"></span> ' + state.text + '</span>'
        );
        return $state;
    }

    function make_key(obj)
    {
        var d = new Date().getTime()+Math.random();
        console.log(d);
        var passwd = hex_md5(""+d);
        console.log(passwd)
        $(obj).parent().parent().find("input").val(passwd);
    }
    layui.use(['layer','laypage',"element"], function(){



        //全选
//        form.on('checkbox(allChoose)', function(data){
//            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
//            child.each(function(index, item){
//                item.checked = data.elem.checked;
//            });
//            form.render('checkbox');
//        });
        $("#info_list").datatables({
            idField: 'order_no',
            mobile_field:["ck","agent_name","status"],
            columns:[
                {field:'ck',checkbox:true },
                {field:'id',title:'编号',width:50},
                {field:'agent_name',title:'代理名称',width:100},
                {field:'agent_info',title:'代理备注',width:100},
                {field:'agent_type',title:'类型',width:50, formatter: function (value, row, index) {
                        if(value===1){
                            return "代理";
                        }else{
                            return "员工代理";
                        }
                    }},

                {field:'balance',title:'余额',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'credit',title:'授信额度',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'total_rebate',title:'累计收益',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'rebate',title:'收益',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'card_fee',title:'卡费',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'freight',title:'运费',width:50, formatter: function (value, row, index) {return (value * 0.0001).toFixed(3) + "";}},
                {field:'money_discount',title:'专属卡代理折扣',width:100},
                {field:'rebate_discount',title:'专属卡代理佣金折扣',width:100},
                {field:'money_discount_0',title:'普通卡代理折扣',width:100},
                {field:'rebate_discount_0',title:'普通卡代理佣金折扣',width:100},
                {field:'sign_key',title:'KEY',width:100},
                {field:'status',title:'状态',width:30,formatter:function (value,row,index) {
                        if(value===1){
                            return "禁用";
                        }else{
                            return "正常";
                        }
                    }},
                {field:'operation',title:'操作',width:30,formatter:function (value,row,index) {
                        var text = "";
                        text += '<div class="btn-group"> ';
                        text += '<a data-toggle="dropdown" class="dropdown-toggle"> ';
                        text += '<i class="fa fa-cog"></i> ';
                        text += '</a> ';
                        text += '<ul role="menu" class="dropdown-menu pull-right"> ';

                        text +='<li><a href="javascript:void 0;" onclick="edit_info(\''+row.id+'\')">编辑</a></li> ' ;
                        text +='<li><a href="javascript:void 0;" onclick="set_credit(\''+row.id+'\')">授信</a></li> ' ;
                        text +='<li><a href="javascript:void 0;" onclick="set_balance(\''+row.id+'\')">加款</a></li> ' ;
                        text +='<li><a href="javascript:void 0;" onclick="h5href(\''+row.id+'\')">h5链接</a></li> ' ;
                        text +='</ul> ';
                        text +='</div>';
                        return text;
                    }}

            ],
            ajax:{
                url:"/agents/ajax_agent_list",
                parems:get_search_data(),
                callback:function (msg) {
                    check_table_list_data(msg);
                    check_parent_height();
                }
            }
        });
    });

    function h5href(id)
    {
        var url = 'https://fuel.win10030.com/?agent_id='+id;
        $("#weixin_code_url .name").html(url);
        $("#weixin_code_url .qrcode").empty();
        $("#weixin_code_url .qrcode").qrcode({width: 200, height: 200, text: url});
        layer.open({
            type: 1,
            title: false,
            area: '316px',
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: $('#weixin_code_url')
        });
        // layer.alert('https://fuel.win10030.com/?agent_id='+id, {icon: 6});
    }
    function set_is_log(obj,id,is_log) {
        AjaxCommen("GET","/adminmenu/ajax_set_sys_menu_is_log",{id:id,is_log:is_log},function (msg) {
            if (!msg.error)
            {
                var text = "";
                if(!is_log) {
                    text = '<a href="javascript:void 0;" onclick="set_is_log(this,'+id+',1)"><span class="fa fa-square-o"></span></a>';
                }else if(is_log == 1){
                    text = '<a href="javascript:void 0;" onclick="set_is_log(this,'+id+',0)"><span class="fa fa-check-square-o"></span></a>';
                }
                $(obj).parent().html(text);
            }
        });
    }
    function set_credit(order_id)
    {
        $("#set_credit form")[0].reset();
        $("#set_credit input[name='id']").val(order_id);
        openDialog2({title:"授信信息",area: ['400', "200"],content:$("#set_credit"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/agents/ajax_agent_set_credit",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function set_balance(order_id)
    {
        $("#set_balance form")[0].reset();
        $("#set_balance input[name='id']").val(order_id);
        openDialog2({title:"加款信息",area: ['400', "200"],content:$("#set_balance"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/agents/ajax_agent_set_balance",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }

    function edit_info(id) {
        $("#edit_info form")[0].reset();
        $("#edit_info #product_list").empty();
        $("#edit_info").find("input[name='id']").val(0);
        $("#edit_info").find("select[name='status']").val(0).trigger("change");

        if(id)
        {
            AjaxCommen("GET","/agents/ajax_agent_info",{id:id},function (msg) {
                if (!msg.error)
                {
                    $("#edit_info input[type!='radio'],#edit_info select,#edit_info textarea").each(function () {
                        var name = $(this).attr("name");

                        if(name&&msg.data[name]!==undefined)
                        {
                            $(this).val(msg.data[name]);

                        }

                        if($(this).is("select"))
                        {
                            $(this).trigger("change");
                        }
                    });

                    $("#edit_info input[name='id']").val(msg.data.id);


                    open_info();
                }
            });
        }else{

            open_info();
        }


    }
    function open_info()
    {
        $("#edit_info select").trigger("change");
        openDialog2({title:"用户信息",area: ['400', "500"],content:$("#edit_info"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/agents/ajax_agent_info_save",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function get_search_data()
    {
        var data = {};

        var agent_name = $("#search_form input[name='agent_name']").val();

        data["agent_name"] = agent_name;


        return data;
    }
    function search()
    {
        var data = get_search_data();
        console.log(data)
        $("#info_list").setParems(data);
        $("#info_list").reload();
    }
    function search_reset() {
        $("#search_form")[0].reset();
        search();
    }



</script>

</body>
</html>